package com.module.app.base

import android.graphics.Paint
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.CallSuper
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.SkinAppCompatDelegateImpl
import androidx.databinding.ViewDataBinding
import com.module.base.base.BaseExceptionViewModel
import com.module.base.base.BaseVmDbActivity
import com.module.base.base.title.OnTitleListener
import com.module.base.utils.DisplayUtils
import com.module.base.utils.StatusMyBarUtil
import com.module.app.R
import com.module.app.base.loadsir.LoadSirReload
import com.module.app.utils.InitializationUtils
import com.module.app.utils.MySkinUtils
import com.module.frame.ext.observe
import com.module.skin.ext.skinFlow
import com.umeng.analytics.MobclickAgent

abstract class BaseActivity<VM : BaseExceptionViewModel, DB : ViewDataBinding> :
    BaseVmDbActivity<VM, DB>(),
    OnTitleListener, LoadSirReload {

    private var ivBg: ImageView? = null

    override fun onResume() {
        super.onResume()
        InitializationUtils.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }

    @CallSuper
    override fun initView() {
        InitializationUtils.init()
        super.initView()
        initTitle()
        setBackground(MySkinUtils.isBg())
    }

    @CallSuper
    override fun initListener() {
        super.initListener()
        //皮肤更换
        observe(skinFlow) {
            setBackground(MySkinUtils.isBg())
        }
    }

    @CallSuper
    override fun reload() {
        super.reload()
    }

    @CallSuper
    override fun refresh() {
        super.refresh()
    }

    /**
     * 设置背景图
     */
    protected open fun setBackground(isBackground: Boolean) {
        if (ivBg == null) {
            ivBg = findViewById(R.id.iv_bg)
        }
        if (isBackground) {
            if (ivBg != null) {
                com.module.app.utils.GlideUtils.loadBg(ivBg?.context, ivBg, MySkinUtils.getBgUrl())
            }
        } else {
            if (ivBg != null) {
                com.module.app.utils.GlideUtils.load(ivBg?.context, ivBg, R.color.transparent, R.color.transparent)
            }
        }
    }

    /**
     * 皮肤处理
     */
    override fun getDelegate(): AppCompatDelegate {
        return SkinAppCompatDelegateImpl.get(this, this)
    }


    /******************************设置标题开始 */
    var tvTitle: TextView? = null
    var btnLeft1: ImageView? = null //返回
    var btnRight1: ImageView? = null//右边按钮
    var tvRight: TextView? = null //右边文字
    var layoutTitle: View? = null
    private var initTitle = false

    override fun initTitle() {
        if (initTitle) {
            return
        }
        initTitle = true
        tvTitle = findViewById(R.id.tv_title)
        layoutTitle = findViewById(R.id.layout_title)
        btnLeft1 = findViewById(R.id.btn_left_1)
        btnRight1 = findViewById(R.id.btn_right_1)
        tvRight = findViewById(R.id.tv_right)
        if (layoutTitle != null) {
            StatusMyBarUtil.setPaddingSmart(getContext(), layoutTitle)
        }
        if (btnLeft1 != null) {
            btnLeft1!!.setOnClickListener { getActivity().onBackPressed() }
        }
    }

    override fun setMyTitle(title: String?) {
        if (tvTitle == null) {
            return
        }
        tvTitle?.text = title
        tvTitle?.paint?.style = Paint.Style.FILL_AND_STROKE
        tvTitle?.paint?.strokeWidth = 0.7f
    }

    override fun setMyTitle(resId: Int) {
        setMyTitle(resources.getString(resId))
    }

    override fun setCommonTitle(title: String?, isBack: Boolean) {
        if (tvTitle == null) {
            return
        }
        setMyTitle(title)
        tvTitle?.gravity = Gravity.LEFT
        setBackVisibility(if (isBack) View.VISIBLE else View.GONE)
        layoutTitle?.setBackgroundResource(R.color.transparent)
        if (isBack) {
            btnLeft1?.apply {
                setPadding(
                    DisplayUtils.dip2px(20f),
                    paddingTop,
                    paddingRight,
                    paddingBottom
                )
            }
            tvTitle?.apply {
                setPadding(
                    DisplayUtils.dip2px(43f),
                    paddingTop,
                    paddingRight,
                    paddingBottom
                )
                setOnClickListener { finish() }
            }

        } else {
            tvTitle?.apply {
                setPadding(
                    DisplayUtils.dip2px(20f),
                    paddingTop,
                    paddingRight,
                    paddingBottom
                )
            }
        }

    }

    override fun setCommonTitle(resId: Int, isBack: Boolean) {
        setCommonTitle(resources.getString(resId), isBack)
    }

    override fun setBackVisibility(visibility: Int) {
        if (btnLeft1 == null) {
            return
        }
        btnLeft1?.visibility = visibility
    }

    override fun setBackResource(resId: Int) {
        if (btnLeft1 == null) {
            return
        }
        btnLeft1?.setImageResource(resId)
    }

    override fun setBackBtn(onClickListener: View.OnClickListener?) {
        if (btnLeft1 == null) {
            return
        }
        btnLeft1?.setOnClickListener(onClickListener)
    }

    override fun setBackBtn(resId: Int, onClickListener: View.OnClickListener?) {
        setBackResource(resId)
        setBackBtn(onClickListener)
    }

    override fun setRightBtn(@DrawableRes resId: Int, onClickListener: View.OnClickListener?) {
        if (btnRight1 == null) {
            return
        }
        btnRight1?.visibility = View.VISIBLE
        btnRight1?.setImageResource(resId)
        btnRight1?.setOnClickListener(onClickListener)
    }

    override fun setRightText(text: CharSequence?, onClickListener: View.OnClickListener?) {
        if (tvRight == null) {
            return
        }
        tvRight?.visibility = View.VISIBLE
        tvRight?.text = text
        tvRight?.setOnClickListener(onClickListener)
    }

    override fun setRightText(@StringRes resId: Int, onClickListener: View.OnClickListener?) {
        setRightText(getContext().resources?.getString(resId), onClickListener)
    }

    override fun setRightBtnVisibility(visibility: Int) {
        if (btnRight1 == null) {
            return
        }
        btnRight1?.visibility = visibility
    }

    override fun setRightTextVisibility(visibility: Int) {
        if (tvRight == null) {
            return
        }
        tvRight?.visibility = visibility
    }

    /******************************设置标题结束*************************************/

}